﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;
using Web_Oto.Data.Dto;

namespace Web_Oto
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetProduct(); 
            }
        }

        //private void GetSeoInformations(string title, string description, string keyword)
        private void GetSeoInformations(ProductDto product)
        {
            //var seoBus = new SeoBus();
            //var seo = seoBus.GetSeoPage("product");
            //if (seo != null)
            //{
            //    Page.Title = seo.Title + " | " + title;
            //    Page.MetaDescription = description + ", " + seo.Description;
            //    Page.MetaKeywords = keyword + ", " + seo.Keyword;
            //}
            //else
            //{
            //    Page.Title = title;
            //    Page.MetaDescription = description;
            //    Page.MetaKeywords = keyword;
            //}
            if (product != null)
            {
                Page.Title = product.SEO_Title;                
                Page.MetaKeywords = product.SEO_Keyword;
                Page.MetaDescription = product.SEO_Description;
            }
        }

        private void GetProduct()
        {
            //var productId = (Page.RouteData.Values.ContainsKey("productId"))
            //                    ? Page.RouteData.GetRequiredString("productId")
            //                    : null;
            var currentUrl = Request.Url.ToString();
            currentUrl = currentUrl.Substring(currentUrl.LastIndexOf("-") + 1);
            var productId = currentUrl.Replace(".html", "");
            if(productId != null)
            {
                var productBus = new ProductBus();
                var product = productBus.GetProductById(int.Parse(productId));
                if(product != null)
                {
                    var path = ConfigurationManager.AppSettings["productPath"];
                    ImgProduct.ImageUrl = path +"/"+ product.Image;
                    lblProductName.Text = product.Name;
                    lblCategoryName.Text = product.CategoryName;
                    lblPrice.Text = product.Price;
                    lblHotline.Text = product.Hotline;
                    ltrIntroProduct.Text = product.Description;

                    //GetSeoInformations(product.Name + " | " + product.CategoryName, product.Name + ", " + product.CategoryName, CommonHelper.NiceUrl(product.Name) + "," + CommonHelper.NiceUrl(product.CategoryName) + ", " + product.Name +"," + product.CategoryName);
                    GetSeoInformations(product);
                    Breakcrum(product.Name, product.CategoryName, product.CategoryParentName);
                    RelativeProducts(int.Parse(productId), productBus);
                }
            }
        }

        private void Breakcrum(string productName, string subCategory = null, string category = null)
        {
            var sb = new StringBuilder();
            var breakLine = "<img src='/images/next-icon.png' alt='' />";
            sb.Append("<a href='/san-pham.html'>Sản phẩm</a>");
            if (!String.IsNullOrWhiteSpace(subCategory) && !String.IsNullOrWhiteSpace(category))
            {
                sb.Append(breakLine);
                sb.AppendFormat("<a href='/san-pham/{0}/{1}/1.html'>{2}</a>", CommonHelper.NiceUrl(category.ToLower()), CommonHelper.NiceUrl(subCategory.ToLower()), subCategory.ToLower());
            }
            sb.Append(breakLine);
            sb.AppendFormat("<span>{0}</span>", productName);
            ltrBreakCrum.Text = sb.ToString();
        }

        private void RelativeProducts(int productId, ProductBus productBus)
        {
            int? showItems = null;
            var products = productBus.GetRelativeProductDto(productId, showItems);
            var sb = new StringBuilder();
            var path = ConfigurationManager.AppSettings["productPath"];
            foreach (var product in products)
            {
                sb.AppendFormat("<li><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='mask_home'><div></div><img src='{4}/{5}' title='{6}' alt='{6}'></a>",
                    CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, path, product.Image, product.TitleOfImage);
                sb.AppendFormat("<div><h5><a href='/san-pham/{0}/{1}/{2}-{3}.html' class='product-name'>{4}</a></h5></div>", CommonHelper.NiceUrl(product.CategoryParentName.ToLower()), CommonHelper.NiceUrl(product.CategoryName.ToLower()),
                    CommonHelper.NiceUrl(product.Name.ToLower()), product.Id, product.Name);
                sb.Append("</li>");
            }
            ltrRelativeProduct.Text = sb.ToString();
        }
    }
}